import tfd.scala.squib._
import tfd.scala.squib.event._
import tfd.scala.squib.jmc._
import tfd.scala.squib.jmc.event._

import java.awt.{BorderLayout, Dimension, FlowLayout}
import java.awt.event.{ActionEvent}
import java.io.{File}
import java.net.{URI}
import javax.swing.{JFileChooser, JFrame, JOptionPane, JPanel, SpinnerNumberModel}
import javax.swing.filechooser._

package tfd.scala.squib.demo.jmc {

    object MediaPaneDemo extends Application {

        lazy val mediaPane = mediapane.id("mediapane")
        lazy val playPause = button.id("play/pause")
        lazy val mute = checkbox.id("mute")
        
        val frm:JFrame = frame(
            'title->"SQUIB Media Player Demo",
              'defaultCloseOperation -> JFrame.EXIT_ON_CLOSE,
              'size-> new Dimension(720,480),
              'layout-> new BorderLayout(),
              'visible-> true,
              'jmenubar -> menubar(
                contents(
                    menu(
                        'text-> "File",
                          'mnemonic->'F',
                          contents(
                            menuitem('text->"Open File",
                                     'mnemonic -> 'O',
                                     actionPerformed {
                                    val chooser = new JFileChooser()
                                    val filter = new FileNameExtensionFilter("Media Files", "mp3, avi", "mpg")
                                    chooser.setFileFilter(filter)
                                    if (chooser.showOpenDialog(frm) == JFileChooser.APPROVE_OPTION) {
                                        loadMedia(chooser.getSelectedFile)
                                    }
                                }
                            ),
                              menuitem('text->"Open URL",
                                       'mnemonic -> 'U',
                                       actionPerformed {
                                            val openUrl = JOptionPane.showInputDialog("Open URL:");
                                            if (openUrl != null) {
                                                loadMedia(openUrl)
                                            }
                                }
                            )
                        )
                    )
                )
            ),
              contents(
                mediapane("mediapane",
                            'volume -> 0.0f,
                            'layout->new BorderLayout(),
                            contents (
                                panel(
                                    'layout->new FlowLayout(),
                                    'opaque->false,
                                    contents(
                                        button("play/pause",
                                                'text->"Play",
                                                actionPerformed {
                                                    if (mediaPane.isPlaying) {
                                                    mediaPane.pause
                                                } else {
                                                    mediaPane.play
                                                }
                                                }),
                                        label('text->"Volume:"),
                                        spinner('model -> new SpinnerNumberModel(10, 0, 10, 1),
                                                spinnerValueChanged { value:Object =>
                                                    mediaPane.setVolume(value.asInstanceOf[java.lang.Integer].intValue / 10f)
                                                }),
                                        label('text->"Mute:"),
                                        checkbox("mute",
                                                'selected->false,
                                                actionPerformed {
                                                    mediaPane.setMute(mute.isSelected)
                                                }),
                                        button("|<",
                                                'text->"|<",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(0.0)
                                                }),
                                        button("<<",
                                                'text->"<<",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(mediaPane.getMediaTime() - 60.0)
                                                }),
                                        button("<",
                                                'text->"<",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(mediaPane.getMediaTime() - 1.0)
                                                }),
                                        button(">",
                                                'text->">",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(mediaPane.getMediaTime() + 1.0)
                                                }),
                                        button(">>",
                                                'text->">>",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(mediaPane.getMediaTime() + 60.0)
                                                }),
                                        button(">|",
                                                'text->">|",
                                                actionPerformed { ae:ActionEvent =>
                                                    mediaPane.setMediaTime(mediaPane.getDuration())
                                                })
                            )) -> BorderLayout.SOUTH),
                            playerStarted {
                                playPause.setText("Pause")
                            },
                            playerStopped {
                                playPause.setText("Play")
                            },
                          endOfMediaReached {
                        mediaPane.setMediaTime(0.0)
                        playPause.setText("Play")
                    }
                ) -> BorderLayout.CENTER,
            )
        )

        def loadMedia(file:File) = mediaPane.loadURI(file.toURI())
        
        def loadMedia(url:String) = mediaPane.loadURI(new URI(url))

        //loadMedia(new File("C:\\tmp\\ShowMeSTLTechnique_cut.mpg"))
        //loadMedia("http://sun.edgeboss.net/download/sun/media/1460825906/1460825906_2957290001_DayEarth-Bluray.flv")
    }
}
